home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / generateLightMaps.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  8.8 KB  |  289 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. // Procedure : generateLightMaps
  19. //
  20. // Description:
  21. //        Generate a set of light maps for a set of
  22. //        geometry.
  23. //
  24. // Arguments:
  25. //        useThisShader : use the shader passed in versus shader 
  26. //        associated with each object
  27. //        postFix : string to stick on to end of light map file texture name
  28. //        allMeshes : all poly geometry
  29. //        shadows : compute shadows
  30. //        antiAlias : anti-alias light map images
  31. //        rx, ry : resolution of light map in x and y
  32. //
  33. // Author : Bernard Kwok
  34. // Last Updated : 08/23/00
  35. //
  36.  
  37. //
  38. // Create a set of dummy shaders for light mapping only
  39. //
  40. global proc
  41. handleDummyLightMapShaders(int $deleteIt)
  42. {
  43.     global string $fakeShaders[];
  44.     
  45.     int $numFake = size($fakeShaders);
  46.     for ($i=0; $i<$numFake; $i++) {
  47.         catch ( `delete $fakeShaders[$i]` );
  48.     }
  49.     
  50.     if ($deleteIt) {
  51.         return;
  52.     }
  53.  
  54.     // Create a dummy lambert shader + shading group
  55.     string $newss = `shadingNode -name _fakeSS_lambert_ -asShader lambert`;
  56.     string $sgName = ($newss + "SG");
  57.     $sgName = `sets -renderable true -noSurfaceShader true -empty -name $sgName`;
  58.     eval ("connectAttr -f " + $newss+".outColor " + $sgName+".surfaceShader");
  59.     eval ("setAttr -type float3 " + $newss+".color 1.0 1.0 1.0");
  60.     $fakeShaders[0] = $newss;
  61.     $fakeShaders[1] = $sgName;
  62.     
  63.     // Create a dummy lambert shader + shading group
  64.     $newss = `shadingNode -name _fakeSS_blinn_ -asShader blinn`;
  65.     $sgName = ($newss + "SG");
  66.     $sgName = `sets -renderable true -noSurfaceShader true -empty -name $sgName`;
  67.     eval ("connectAttr -f " + $newss+".outColor " + $sgName+".surfaceShader");
  68.     eval ("setAttr -type float3 " + $newss+".color 1.0 1.0 1.0");
  69.     $fakeShaders[2] = $newss;
  70.     $fakeShaders[3] = $sgName;
  71.     
  72.     // Create a dummy lambert shader + shading group
  73.     $newss = `shadingNode -name _fakeSS_phong_ -asShader phong`;
  74.     $sgName = ($newss + "SG");
  75.     $sgName = `sets -renderable true -noSurfaceShader true -empty -name $sgName`;
  76.     eval ("connectAttr -f " + $newss+".outColor " + $sgName+".surfaceShader");
  77.     eval ("setAttr -type float3 " + $newss+".color 1.0 1.0 1.0");
  78.     $fakeShaders[4] = $newss;
  79.     $fakeShaders[5] = $sgName;
  80. }
  81.  
  82. global proc 
  83. generateLightMaps(string $useThisShader,
  84.                   string $postFix,
  85.                   string $fileFormat,
  86.                   int $allMeshes,
  87.                   int $blendMode,
  88.                   int $shadows,
  89.                   int $antiAlias,
  90.                   int $rx, int $ry)
  91. {
  92.     global string $fakeShaders[];
  93.     
  94.     // Get what to work on
  95.     string $selected[];
  96.     int $needsUVRange[]; // Only polys should use the 0 1 0 1 uv range ?
  97.     int $cnt = 0;
  98.     
  99.     // All untemplated meshes / subdivs / nurbs
  100.     if ($allMeshes) {
  101.         $selected = `ls -type "mesh" -type "subdiv" -type "nurbsSurface" -ut`;
  102.         select -r $selected;
  103.         $cnt = size($selected);
  104.         for ($i=0; $i<$cnt; $i++)
  105.         {
  106.             $needsUVRange[$i] = 1;
  107.         }
  108.     }
  109.     
  110.     // Check the selection list, and screen out meshes / subdivs / nurbs
  111.     else {
  112.         string $unselect[];
  113.         $cnt = 0;
  114.  
  115.         string $unfiltered[] = `ls -dag -ap -sl`;
  116.         for ($i=0; $i<size($unfiltered); $i++) {
  117.             string $item = $unfiltered[$i];
  118.             string $ntype = `nodeType $item`;
  119.             //print ("Node check " + $item + " = " + $ntype + "\n");
  120.             if ($ntype == "mesh") {
  121.                 $selected[$cnt] = $item;
  122.                 $needsUVRange[$cnt] = 1;
  123.                 $cnt++;
  124.             }
  125.             else if ($ntype == "nurbsSurface") {
  126.                 $selected[$cnt] = $item;
  127.                 $needsUVRange[$cnt] = 0;
  128.                 $cnt++;
  129.             }
  130.             else if ($ntype == "subdiv") {
  131.                 $selected[$cnt] = $item;
  132.                 $needsUVRange[$cnt] = 0;
  133.                 $cnt++;
  134.             }
  135.         }
  136.     }
  137.  
  138.     if ($cnt == 0) {
  139.         error ("No poly meshes selected. Operation not performed.");
  140.         return;
  141.     }
  142.  
  143.     // Default to IFF if no format was given
  144.     if ($fileFormat == "")
  145.         $fileFormat = "iff";
  146.     
  147.     // Remove any shader duplicates
  148.     print("// Removing any shader instances...\n");
  149.     removeObjectShaderInstances $selected;
  150.     
  151.     // Toggle off the material mappings
  152.     print("// Disabling shader textures...\n");
  153.     toggleMaterialMapping 0;
  154.     
  155.     // Create fake shaders
  156.     print("// Generating fake shaders...\n");
  157.     handleDummyLightMapShaders( 0 );
  158.     
  159.     //print ("// **** Start working on objects: { **********\n");
  160.     //print $selected;
  161.     //print ("// }\n");
  162.     
  163.     // Get the shaders associated with each geometry,
  164.     // and run "light map" generation on it.
  165.     for ($i=0; $i<$cnt; $i++) {
  166.         
  167.         string $object = $selected[$i];
  168.         string $shaders[] = `getConnectedShaders $object 0`;
  169.         int $numShaders = size($shaders);
  170.         
  171.         string $uvRangeStr = " -uvRange 0 1 0 1 ";
  172.          string $noUVRangeStr = " ";
  173.         if ($needsUVRange[$i] == 0)
  174.             $uvRangeStr = $noUVRangeStr;
  175.         
  176.         // Let generate those light maps baby !
  177.         if ($numShaders) {
  178.             
  179.             // Remove any path seperators and replace them with "_" characters.
  180.             string $testName1 = `substitute "|" $object "_"`;
  181.             string $testName2 = `substitute "|" $testName1 "_"`;
  182.             // print ("// Testnames = " + $testName1 + " " + $testName2 + "\n");
  183.             while ($testName1 != $testName2) {
  184.                 $testName1 = $testName2;
  185.                 $testName2 = `substitute "|" $testName1 "_"`;        
  186.             }
  187.             string $fileName;
  188.             for ($j=0; $j<$numShaders; $j++) {
  189.                 
  190.                 string $shader = $shaders[$j];
  191.                 
  192.                 //print("** generating lmap for object " + $object + ", shader: " + $shader + "\n");
  193.                 
  194.                 $fileName = $testName1 + "_" + $shader + "_" + $postFix;
  195.                 
  196.                 print("// Generating lightmap: " + $fileName + " for object " + $object + "\n");
  197.                 
  198.                 // New texture name
  199.                 string $txtName[];
  200.                 int $cstHasBug = 1;
  201.                 
  202.                 // Use the user passed in shader
  203.                 if (size($useThisShader) != 0) {
  204.                     $txtName = `evalEcho ("convertSolidTx -aa " +
  205.                                           $antiAlias + 
  206.                                           " -rx " + $rx + " -ry " + $ry + 
  207.                                           " -sh " + $shadows + 
  208.                                           " -fin " + $fileName + 
  209.                                           " -n " + $fileName + 
  210.                                           " -fileFormat " + $fileFormat + 
  211.                                           $uvRangeStr + 
  212.                                           $object + " " + $useThisShader)`;          
  213.                 }
  214.                 else {
  215.                     
  216.                     // Generate the lightmap. Notice that we only want
  217.                     // the 0,0 to 1 1 uvrange at all times.
  218.                     //
  219.                     if ($cstHasBug == 1) {
  220.                         // Creata a dummy shader of the correct type to eval lighting with
  221.                         // since there the object may be assigned to a shader which
  222.                         // has been duplicated + hooked to a texture.
  223.                         //
  224.                         // Get the surface shader
  225.                         //
  226.                         string $stype[];
  227.                         $stype = `ls -showType $shader`;
  228.                         if ($stype[1] == "shadingEngine") {
  229.                             string $sshader[] = eval ("listConnections -s 1 " + $shader + ".surfaceShader;");
  230.                             if (size($sshader)) {
  231.                                 $stype = `ls -showType $sshader[0]`;
  232.                                 string $fakeName;
  233.                                 if ($stype[1] == "lambert") {
  234.                                     $fakeName = $fakeShaders[1];
  235.                                 }
  236.                                 else if ($stype[1] == "blinn") {
  237.                                     $fakeName = $fakeShaders[3];
  238.                                 }
  239.                                 if ($stype[1] == "phong") {
  240.                                     $fakeName = $fakeShaders[5];                
  241.                                 }
  242.                                 
  243.                                 $txtName = `evalEcho ("convertSolidTx -aa " +
  244.                                                       $antiAlias + 
  245.                                                       " -rx " + $rx + " -ry " + $ry + 
  246.                                                       " -sh " + $shadows + 
  247.                                                       " -fin " + $fileName + 
  248.                                                       " -n " + $fileName + 
  249.                                                       " -fileFormat " + $fileFormat + 
  250.                                                       $uvRangeStr + 
  251.                                                       $object + " " + $fakeName)`;          
  252.                             }
  253.                         }
  254.                     }
  255.                     else {
  256.                         $txtName = `evalEcho ("convertSolidTx -aa " +
  257.                                               $antiAlias + 
  258.                                               " -rx " + $rx + " -ry " + $ry + 
  259.                                               " -sh " + $shadows + 
  260.                                               " -fin " + $fileName + 
  261.                                               " -fileFormat " + $fileFormat + 
  262.                                               " -n " + $fileName + 
  263.                                               $uvRangeStr + 
  264.                                               $object + " " + $shader)`;          
  265.                     }
  266.                 }
  267.                 
  268.                 // Now hook the thing up
  269.                 if (size($txtName)) {
  270.                     addTextureToShaderLayered $shader $txtName[0] $blendMode 1 0;
  271.                     
  272.                 }
  273.             }
  274.         }
  275.         else {
  276.             warning ("There are no shaders for object: " + $object + ". Nothing done.");
  277.         }
  278.     }
  279.     
  280.     // Delete fake shaders
  281.     handleDummyLightMapShaders( 1 );
  282.     
  283.     // Toggle on the material mappings
  284.     select -r $selected;
  285.     toggleMaterialMapping 1;
  286.     print ("// Light map generation finished.\n");
  287. }
  288.  
  289.